package com.sumer.psychology.space.infrastructure.messaging.rabbitmq;

import com.google.common.collect.ImmutableMap;
import com.sumer.psychology.common.port.adapter.messaging.Exchanges;
import com.sumer.psychology.common.port.adapter.messaging.rabbitmq.ExchangeListener;
import com.sumer.psychology.space.domain.FriendMessageType;
import com.sumer.psychology.space.infrastructure.persistence.mongo.friend.MongoFriendMessageRepository;
import com.sumer.psychology.space.infrastructure.persistence.mongo.MongoMessageRepository;

import java.util.Map;

/**
 * 朋友关系事件
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/12/9
 * Time: 14:52
 */
public class FriendMessageCreatedEventListener extends ExchangeListener {
    private static final Map<String, FriendMessageType> FRIEND_MESSAGE_TYPE_MAP = ImmutableMap.of(
            "com.sumer.psychology.friend.domain.model.FriendAddedEvent", FriendMessageType.ADD,
            "com.sumer.psychology.friend.domain.model.FriendDeletedEvent", FriendMessageType.DELETE,
            "com.sumer.psychology.friend.domain.model.FriendRequestedEvent", FriendMessageType.REQUEST,
            "com.sumer.psychology.friend.domain.model.FriendRejectedEvent", FriendMessageType.REJECT
    );

    @Override
    protected String exchangeName() {
        return Exchanges.DEFAULT_EXCHANGE_NAME;
    }

    @Override
    protected void filteredDispatch(String aType, String aTextMessage) {
        MongoMessageRepository mongoMessageRepository = MongoFriendMessageRepository
                .create(FRIEND_MESSAGE_TYPE_MAP.get(aType));
        mongoMessageRepository.add(aTextMessage);
    }

    @Override
    protected String[] listensTo() {
        return new String[]{
                "com.sumer.psychology.friend.domain.model.FriendAddedEvent",
                "com.sumer.psychology.friend.domain.model.FriendDeletedEvent",
                "com.sumer.psychology.friend.domain.model.FriendRequestedEvent",
                "com.sumer.psychology.friend.domain.model.FriendRejectedEvent"};
    }
}
